package com.hngy.xpq.android_practice;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hngy.xpq.android_practice.bean.Merchant;
import com.hngy.xpq.android_practice.database.SQLiteMerchant;
import com.hngy.xpq.android_practice.http.TcpClient;
import com.hngy.xpq.android_practice.http.map.MerchantAddMap;

public class MerchantRegist extends AppCompatActivity {

    EditText name,key1,key2,phone,des;
    TextView add;
    Button zc,hq;
    SQLiteMerchant sqLiteMerchant;
    SharedPreferences merchants,map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.merchant_zc_regist);
        IndexMerchant();
        sqLiteMerchant = new SQLiteMerchant(this);
        zc = findViewById(R.id.button_zc);
        zc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Merchant m = new Merchant();
                if (TextUtils.isEmpty(name.getText())){
                    IndexToast("用户名不能为空!!");
                } else if (TextUtils.isEmpty(key1.getText())){
                    IndexToast("密码不能为空!!");
                } else if (TextUtils.isEmpty(key2.getText())){
                    IndexToast("确认密码不能为空!!");
                } else if (!key1.getText().toString().equals(key2.getText().toString())){
                    IndexToast("两次密码不一致!!");
                } else if (TextUtils.isEmpty(phone.getText())){
                    IndexToast("联系方式不能为空!!");
                } else if (TextUtils.isEmpty(hq.getText())){
                    IndexToast("店址不能为空!!");
                } else {
                    m.setName(name.getText().toString());
                    m.setKey(key1.getText().toString());
                    m.setPhone(phone.getText().toString());
                    m.setAdd(add.getText().toString());
                    m.setDes(des.getText().toString());
                    String information = "ZC002//" + m.getName() + "//" + m.getKey() + "//" +
                            m.getPhone() + "//" + map.getString("bdLocation" , "") + "," + map.getString("longitude" , "") + "//" + m.getDes();
                    String a = new TcpClient().Transits(information);
                    IndexToast(a);
                    if (a.equals("注册成功!") && sqLiteMerchant.Merchant_add(m)){
                        SharedPreferences.Editor editor = merchants.edit();
                        editor.putString("phone",phone.getText().toString());
                        editor.putString("key",key1.getText().toString());
                        editor.putString("name",name.getText().toString());
                        editor.commit();
                        SharedPreferences.Editor editor1 = map.edit();
                        editor1.putString("bdLocation" , "");
                        editor1.putString("longitude" , "");
                        editor1.commit();
                        Intent intent = new Intent(MerchantRegist.this,MerchantActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }
            }
        });

        hq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MerchantRegist.this, MerchantAddMap.class);
                startActivity(intent);
            }
        });
    }

    private void IndexMerchant() {
        name = findViewById(R.id.merchant_xpq_name);
        key1 = findViewById(R.id.merchant_xpq_key1);
        key2 = findViewById(R.id.merchant_xpq_key2);
        phone = findViewById(R.id.merchant_xpq_phone);
        add = findViewById(R.id.merchant_xpq_add);
        des = findViewById(R.id.merchant_xpq_des);
        hq = findViewById(R.id.merchant_add_hq);
        merchants = super.getSharedPreferences("merchants",MODE_PRIVATE);
        map = super.getSharedPreferences("map",MODE_PRIVATE);
        if (!map.getString("bdLocation" , "").equals("")){
            hq.setText("经度:" + map.getString("bdLocation","") + "\n纬度:" + map.getString("longitude" , ""));
        }

    }

    public void IndexToast(String text){
        Toast.makeText(MerchantRegist.this,text,Toast.LENGTH_SHORT).show();
    }
}